/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.seg.converters;

import edu.upn.sigemfi.seg.be.Departamento;
import edu.upn.sigemfi.seg.beans.AbstractController;
import edu.upn.sigemfi.seg.beans.UbigeoController;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Administrador
 */
public class DepartamentoConverter implements Converter {

    public DepartamentoConverter() {
    }

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
        if (string == null) {
            return null;
        }
        try {
            return ((UbigeoController) AbstractController.getBean("UbigeoController")).buscarDepartamento(string);
        } catch (Exception e) {
            return null;
        }
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof Departamento) {
            return ((Departamento) object).getIdDepartamento().toString();
        } else {
            throw new IllegalArgumentException("Se encontro un tipo de dato incorrecto");
        }
    }
}
